package org.csu.hotelsys.controller;

import org.csu.hotelsys.domain.Journal;
import org.csu.hotelsys.service.JournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/journal/")
public class JournalController {
    @Autowired
    JournalService journalService;

    //进入日志界面
    @GetMapping("main")
    public String control(){
        return "templates/journal/main";
    }

    /*-------------------------------------------------日志管理----------------------------*/
    //日志管理主界面,包含查看日志、修改日志信息
    @GetMapping("journalMain")
    public String journalMain( ){
        return "templates/journal/journalMain";
    }


    //修改或注销预定信息界面,界面信息为所有未取消未入住预定单
    @GetMapping("showjournal")
    public String changeReserve(Model model) {

        List <Journal> journalList=journalService.getJournalList();
        model.addAttribute("journalList",journalList);
        return "templates/journal/journal";
    }

    //搜索栏，根据操作时间显示日志记录
    @PostMapping("searchByTime")
    public String searchByTime(String time, Model model) {

        List <Journal> journalList=journalService.getJournalListByTime(time);
        model.addAttribute("journalList",journalList);
        return "templates/journal/journal";
    }

    //搜索栏，根据操作者显示日志记录
    @PostMapping("searchByOperater")
    public String searchByOperater(String operater, Model model) {

        List <Journal> journalList=journalService.getJournalListByOperater(operater);
        model.addAttribute("journalList",journalList);
        return "templates/journal/journal";
    }

    //搜索栏，根据操作时间类型日志记录
    @PostMapping("searchByType")
    public String searchByType(String type,Model model){
        List <Journal> journalList=journalService.getJournalListByType(type);
        model.addAttribute("journalList",journalList);
        return "templates/journal/journal";
    }

    //删除日志
    @GetMapping("deleteJournal")
    public String deleteJournal(String journal_id, Model model) {
        Journal journal = journalService.getJournalByID(journal_id);
        journalService.deleteJournalByID(journal_id);
        List<Journal> journalList=journalService.getJournalList();
        String msg="成功删除日志记录";
        model.addAttribute("msg",msg);
        model.addAttribute("journalList",journalList);
        return "templates/journal/journal";
    }

    @GetMapping("updateConsumption")//这个是在修改日志信息
    public String updateJournal(Journal journal, Model model) {
        journalService.updateOperateInfo(journal.getOperate_info());
        journal=journalService.getJournalByID(journal.getJournal_id());
        model.addAttribute("journal",journal);
        model.addAttribute("msg","修改成功");
        return "templates/journal/update";
    }

}
